package com.startpro.model;

import android.util.Log;

import com.startpro.bean.WeatherInfo;
import com.startpro.bean.WeatherResult;
import com.startpro.iface.Weatherface;
import com.startpro.listener.WeatherListener;
import com.startpro.retrofitReques.WeatherRequest;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by acer on 2017/5/13.
 */

public class WeatherModel implements Weatherface{
  Retrofit retrofit;
    private  static  final String BASE_URL="http://www.weather.com.cn/data/sk/101070201.html";
    public WeatherModel(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)	//主机地址
                .addConverterFactory(GsonConverterFactory.create())//解析方法
                .build();

    }

    @Override
    public void getWeather(String cityNumber, final WeatherListener listener) {
        final WeatherRequest request;
        request = retrofit.create(WeatherRequest.class);
        Call<WeatherResult> call;
        call = request.getResult(cityNumber);
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if(response.isSuccessful()&& response.body()!=null){

                    listener.onResponse(response.body().getWeatherinfo());
                }
                else{
                    listener.onFail("解析错误");
                }
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
            listener.onFail(t.toString());
            }
        });

    }
}
